package com.xunyji.xunyjitest.comm.util;

import com.xunyji.xunyjitest.config.baidu.BaiduBaseConfig;
import com.xunyji.xunyjitest.model.baidu.TransResult;
import com.xunyji.xunyjitest.model.baidu.TranslationResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author AltEnter
 * @create 2019-01-09 10:28
 * @desc 百度翻译API封装
 **/
@Component
@Slf4j
public class BaiduTransApi {

    @Autowired
    private HttpMethodUtils httpMethodUtils;

    @Autowired
    private BaiduBaseConfig baiduBaseConfig;

    /**
     * 发送get请求进行翻译
     * @param query
     * @param from
     * @param to
     * @return
     */
    public String getTransStrResult(String query, String from, String to) throws UnsupportedEncodingException {
        Map<String, String> params = buildParams(query, from, to);
        // 调用自定义的get方法
        String restStr = httpMethodUtils.doGetStrWithMapParamByRestTemplate(baiduBaseConfig.getTranslation().getTransApiHost(), params);
        String result = TransformUtils.unicodeToUtf8(restStr);
        TranslationResponse translationResponse = TransformUtils.parseStr2Object(result, TranslationResponse.class);
        log.info("getTransStrResult - 翻译请求结果为：" + translationResponse.toString());
        TransResult transResult = translationResponse.getTrans_result()[0];
        return String.format("%s -> %s", transResult.getSrc(), transResult.getDst());
        // 调用官方提供的get方法
//        return HttpGet.get(baiduBaseConfig.getTranslation().getTransApiHost(), params);
    }

    public Map<String, String> getTransMapResult(String query, String from, String to) throws UnsupportedEncodingException {
        Map<String, String> params = buildParams(query, from, to);
        // 调用自定义的get方法
        Map<String, String> restMap = httpMethodUtils.doGetMapWithMapByRestTemplate(baiduBaseConfig.getTranslation().getTransApiHost(), params);
        String trans_result = restMap.get("trans_result");
        return restMap;
        // 调用官方提供的get方法
//        return HttpGet.get(baiduBaseConfig.getTranslation().getTransApiHost(), params);
    }



    /**
     * 构建请求参数
     * @param query
     * @param from
     * @param to
     * @return
     */
    private Map<String, String> buildParams(String query, String from, String to) {

        String appid = baiduBaseConfig.getTranslation().getAppId();
        String securityKey = baiduBaseConfig.getTranslation().getSecurityKey();

//        String appid = "20190109000255665";
//        String securityKey = "iNPVhM9qtM3Kb0ZTesI9";

        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", appid);

        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);

        // 签名
        String src = appid + query + salt + securityKey; // 加密前的原文
        log.info("buildParams - 加密前的签名：" + src);
        String sign = Md5Utils.md5(src);
        params.put("sign", sign);
        log.info("buildParams - 加密后的签名：" + sign);

        return params;
    }



}

